package com.lh1010.s3.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lh1010.s3.repository.entity.McUser;
import com.lh1010.s3.repository.mapper.McUserMapper;
import com.lh1010.s3.vo.McUserPageSearchVo;
import com.lh1010.s3.vo.McUserVo;
import com.zielsmart.web.basic.exception.ValidateException;
import com.zielsmart.web.basic.util.ConvertUtils;
import com.zielsmart.web.basic.vo.PageVo;
import com.zielsmart.web.basic.vo.TokenUserInfo;
import org.beetl.sql.core.DynamicSqlManager;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;
import org.beetl.sql.core.page.PageResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.apache.poi.ss.usermodel.Workbook;
import java.util.Base64;
import java.util.List;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 王帅杰
 * @version 2.0
 * @package com.zielsmart.mc.service
 * @title PlatformPropertiesService
 * @description
 * @date 2021-08-09 15:55:42
 * @Copyright 2019 www.zielsmart.com Inc. All rights reserved
 * 注意：本内容仅限于郑州致欧网络科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
@Service
@Slf4j
public class McUserService {
    
    @Resource
    private McUserMapper mcUserMapper;
    @Resource
    private DynamicSqlManager dynamicSqlManager;

    /**
     * queryByPage
     * 分页查询
     * @param searchVo
     * @return {@link com.zielsmart.web.basic.vo.PageVo<com.zielsmart.mc.vo.McUserVo>}
     * @author 王帅杰
     * @history
     */
    public PageVo<McUserVo> queryByPage(McUserPageSearchVo searchVo) {
        PageRequest pageRequest = DefaultPageRequest.of(searchVo.getCurrent(), searchVo.getPageSize());
        //TODO 自行修改SQL条件查询
        PageResult<McUserVo> pageResult = dynamicSqlManager.getMapper(McUserMapper.class).queryByPage(searchVo, pageRequest);
        return ConvertUtils.pageConvert(pageResult, McUserVo.class, searchVo);
    }

    /**
     * save
     * 添加
     * @param mcUserVo
     * @param tokenUser
     * @throws {@link ValidateException}
     * @author 王帅杰
     * @history
     */
    public void save(McUserVo mcUserVo, TokenUserInfo tokenUser) throws ValidateException {
        if (ObjectUtil.isEmpty(mcUserVo)) {
            throw new ValidateException("数据存在空值，请检查数据");
        }
        mcUserVo.setAid(IdUtil.fastSimpleUUID());
        //TODO 根据情况判断是否需要添加创建人信息
        mcUserMapper.insert(ConvertUtils.beanConvert(mcUserVo, McUser.class));
    }

    /**
     * update
     * 修改
     * @param mcUserVo
     * @param tokenUser
     * @throws {@link ValidateException}
     * @author 王帅杰
     * @history
     */
    public void update(McUserVo mcUserVo, TokenUserInfo tokenUser) throws ValidateException {
        if (ObjectUtil.isEmpty(mcUserVo) || StrUtil.isEmpty(mcUserVo.getAid())) {
            throw new ValidateException("Aid不能为空");
        }
        //TODO 根据情况判断是否需要设置修改人信息
        mcUserMapper.createLambdaQuery()
                .andEq("aid",mcUserVo.getAid())
                .updateSelective(ConvertUtils.beanConvert(mcUserVo, McUser.class));
    }

    /**
     * delete
     * 删除
     * @param mcUserVo
     * @throws {@link ValidateException}
     * @author 王帅杰
     * @history
     */
    public void delete(McUserVo mcUserVo) throws ValidateException {
        if (ObjectUtil.isEmpty(mcUserVo)) {
            throw new ValidateException("请选择要删除的数据");
        }
        //TODO 是否使用批量删除
        // mcUserMapper.createLambdaQuery().andIn("aid", mcUserVo.getAidList()).delete();
        mcUserMapper.createLambdaQuery().andEq("aid",mcUserVo.getAid()).delete();
    }

    public String export(McUserPageSearchVo searchVo) {
            searchVo.setCurrent(1);
            searchVo.setPageSize(Integer.MAX_VALUE);
            List<McUserVo> records = queryByPage(searchVo).getRecords();
            if (!records.isEmpty()) {
                Workbook workbook = null;
                try {
                    ExportParams params = new ExportParams(null, "管理");
                    params.setType(ExcelType.XSSF);
                    params.setAutoSize(true);
                    workbook = ExcelExportUtil.exportExcel(params, McUserVo.class, records);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    workbook.write(bos);
                    byte[] barray = bos.toByteArray();
                    return Base64.getEncoder().encodeToString(barray);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
}
